﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows.Controls;
using System.Windows.Media.Effects;

namespace TheOliver.Controls
{
    public partial class ClipClapDisplay : UserControl
    {
        public ClipClapDisplay()
        {
            InitializeComponent();
        }

        private string _text;
        public string Text
        {
            get { return _text; }
            set { SetNewText(value);}
        }

        private double _characterMargin = 4;
        public double CharacterMargin
        {
            get { return _characterMargin; }
            set { _characterMargin = value; }
        }

        //private bool _fixedSize = false;
        //public bool FixedSize
        //{
        //    get { return _fixedSize; }
        //    set
        //    {
        //        _fixedSize = value;
        //    }
        //}

        //private double _charSizeWidth = 50;
        //public double CharSizeWidth
        //{
        //    get { return _charSizeWidth; }
        //    set { _charSizeWidth = value; }
        //}

        //private double _charSizeHeight = 100;
        //public double CharSizeHeight
        //{
        //    get { return _charSizeHeight; }
        //    set { _charSizeHeight = value; }
        //}

        private void SetNewText(string value)
        {
            if (value == null)
            {
                return;
            }

            _text = value;
            _helper.Text = value;

            for (int i = 0; i < value.Length; i++)
            {
                // Does character position exist
                ClipClapChar ccc;

                if (_characterStack.Children.Count > i)
                {
                    ccc = _characterStack.Children[i] as ClipClapChar;
                }
                else
                {
                    ccc = new ClipClapChar();
                    ccc.Margin = new System.Windows.Thickness(_characterMargin);

                    // Drop Shadow
                    DropShadowEffect dse = new DropShadowEffect();
                    ccc.Effect = dse;

                    _characterStack.Children.Add(ccc);
                }
                ccc.Character = _text.Substring(i, 1);
            }

            // remove characters
            while (_characterStack.Children.Count > _text.Length)
            {
                _characterStack.Children.RemoveAt(_characterStack.Children.Count-1);
            }

            //if (_fixedSize)
            //{
            //    foreach (ClipClapChar ccc in _characterStack.Children.OfType<ClipClapChar>())
            //    {
            //        ccc.SetValue(FrameworkElement.WidthProperty, _charSizeWidth);
            //        ccc.SetValue(FrameworkElement.HeightProperty, _charSizeHeight);
            //    }
            //}
            //else
            //{
            //    foreach (ClipClapChar ccc in _characterStack.Children.OfType<ClipClapChar>())
            //    {
            //        ccc.SetValue(FrameworkElement.WidthProperty, double.NaN);
            //        ccc.SetValue(FrameworkElement.HeightProperty, double.NaN);
            //    }
            //}
        }
    }
}